package controller.categories;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.hibernate.Session;
import org.hibernate.Transaction;

import categories.Category;

import utils.HibernateUtils;

public class AddCategoriesAction extends Action {

private static Session s = null;
	
	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest req, HttpServletResponse res) throws Exception {
		String resultat = null;
		String name = ((AddCategoriesForm) form).getName();
		String description = ((AddCategoriesForm) form).getDescription();
		
		s = HibernateUtils.getSession();
		Transaction t = s.beginTransaction();
		
		if(name != null && description != null  )
		{
			Category c = new Category();
			c.setName(name);
			c.setDescription(description);
		
			s.save(c);
			t.commit();
			s.close();
			resultat = "succes";
		}
		else
		{
			resultat = "echec";
		}
		
		
		return mapping.findForward(resultat);
	}
}
